package cn.change.org.service.impl;
import cn.change.basic.service.impl.BaseServiceImpl;
import cn.change.basic.util.MD5Utils;
import cn.change.basic.util.StrUtils;
import cn.change.org.domain.Employee;

import cn.change.org.mapper.EmployeeMapper;
import cn.change.org.service.IEmployeeService;
import cn.change.user.domain.Logininfo;
import cn.change.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author 16252
 * @Transactional事物的传播机制：保证一组操作只有一个操作
 * 部门服务实现类
 */
@Service()
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    LogininfoMapper logininfoMapper;
    @Autowired
    EmployeeMapper employeeMapper;
    @Override
    public void add(Employee employee) {
        initEmployee(employee);
        Logininfo logininfo =employee2Logininfo(employee);
        logininfoMapper.save(logininfo);
        employee.setLogininfo_id(logininfo.getId());
        employeeMapper.save(employee);

    }

    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        return logininfo;
    }


    private void initEmployee(Employee employee) {
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);
    }

    @Override
    public void update(Employee employee) {
        super.update(employee);
    }

    @Override
    public void remove(Long id) {
        super.remove(id);
    }

}
